import api from "@/api/apiConfig";
import ajax from "@/api/http";
import store from "@/store";
import crypto from "@/lib/crypto";
import router from "@/router";
import { message } from "ant-design-vue";

export default {
  goPage(routerName, params) {
    router.push({ name: routerName, params: { id: params } });
  },
  // 是否为pc端
  isPC() {
    let flag = true;
    let userAgentInfo = navigator.userAgent;
    let Agents = [
      "Android",
      "iPhone",
      "SymbianOS",
      "Windows Phone",
      "iPad",
      "iPod"
    ];
    for (var v = 0; v < Agents.length; v++) {
      if (userAgentInfo.indexOf(Agents[v]) > 0) {
        flag = false;
        break;
      }
    }
    let state = sessionStorage.getItem("state");
    if (state) {
      store.replaceState(JSON.parse(state));
    }
    store.dispatch("changeIsPC", flag);
  },
  // 获取企业信息
  getCompanyInfo(cb) {
    ajax
      .post(api.getCompanyList)
      .then(res => {
        let status = res.data.status;
        store.dispatch("changeCheckStatus", status.toString());
        store.dispatch("changeValidateMobile", res.data.safemobile);
        store.dispatch("changeCompany", res.data.company_name);
        store.dispatch("changeMobile", res.data.mobile);
        cb && cb();
      })
      .catch(err => {
        message.error(err);
      });
  },
  // 登录
  login(values) {
    if (!values.agreement) {
      message.success("尚未同意协议!");
      return false;
    } else {
      let obj = JSON.parse(JSON.stringify(values));
      delete obj.agreement;
      ajax
        .post(api.login, obj)
        .then(res => {
          localStorage.setItem("ctoken", crypto.setAES(res.token));
          store.dispatch("changeCompany", res.data.company_name);
          store.dispatch("changeMobile", res.data.mobile);
          store.dispatch("changeValidateMobile", res.data.safemobile);
          store.dispatch("changeCheckStatus", res.data.status.toString());
          // 如果已审核为认证就跳转至认证手机页面
          if (res.data.status.toString() === "5") {
            if (res.data.safemobile !== "YES") {
              router.push({ name: "authentication" });
            } else {
              router.push({ name: "home" });
            }
          } else {
            router.push({ name: "home" });
          }
        })
        .catch(err => {
          message.error(err);
        });
    }
  },
  // 登出
  logout() {
    ajax
      .post(api.logout)
      .then(() => {
        sessionStorage.clear();
        localStorage.clear();
        router.push({ name: "login" });
      })
      .catch(err => {
        message.error(err);
      });
  },
  // 获取余额api
  getBalanceApi() {
    ajax
      .post(api.getBalance, null, true)
      .then(res => {
        if (res.money || res.money === 0) {
          store.dispatch("changeBalance", parseFloat(res.money));
        }
      })
      .catch(err => {
        message.error(err);
      });
  },
  // 获取信息数api
  getMesNum() {
    ajax
      .post(api.getMsg, null, true)
      .then(res => {
        store.dispatch("changeReadNum", res.num);
      })
      .catch(err => {
        message.error(err);
      });
  }
};
